<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Customer;
use App\User;
use DB;
use Illuminate\Support\Facades\Validator;
class CustomerController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }
    public function index(Request $request){
        $data=$request->all();
        if(!empty($data['uuid'])){
            $uuid=$data['uuid'];
            $user=User::where('uuid',$uuid)->first();
            if(!empty($user)){
                return view('addcustomer')->with('user',$user);
            }
            else
            {
                print('您的二维码已经失效，请联系管理员13635456575');
            }
        }
        else{
            print('您的二维码已经失效，请联系管理员13635456575');
        }
    }
    public function addCustomer(Request $request){
        $url='https://creditcard.cmbc.com.cn/wsv2/smp_web/mgm/recomAccept.html?rKey=b2774f236dc266c6284bcaed7dd00c6562d971e8eb55c8a38af1f03cc9d7e078c5e925952f50c3b22ce1fa418134679c&rpKey=a9a5f65c4177beca4502ed9f492ebaff3155e1b99fa7ae14e9f5a5882ce98fd4c6dec5f07ca833a4ed93cd083b610d92bf5df8e22ce6e392d1051c6eb13b7f88&ct=1542254519070';
        $data=$request->all();
        $customer=new Customer();
        $customer->name=$data['name'];
        $customer->phone=$data['phone'];
        $customer->idcard=$data['idcard'];
        $customer->userid=$data['userid'];
        $customer->username=$data['username'];
        if(empty(Customer::where('idcard',$customer->idcard)->first())){
            $customer->save(); 
            return redirect($url);
        }else
        {
            print('客户已经添加,如有疑问联系管理员');
        }
    }
    //列表
    public function customerlist(Request $request){
        $data=$request->all();
        $user=$request->user();
        $wheres=array();
        if($user->role=='admin'){
            if(!empty($data['verify'])){
                array_push($wheres,['verify',$data['verify']]);
            }
            if(!empty($data['userid'])){
                array_push($wheres,['userid',$data['userid']]);
            }
        }
        else{
            array_push($wheres,['userid',$user->id]);
        }
        $customers=Customer::where($wheres)->paginate(6);
        $count=Customer::where($wheres)->count();
        $search_userid='';
        $search_verify='';
        foreach($wheres as $where){
            switch ($where[0]) {
            case 'userid':
                $search_userid=$where[1];
                break;
            case 'veryfi':
                $search_verify=$where[1];
                break;
            default:
                // code...
                break;
            }
        }
        return view('customerlist',['customers'=>$customers,'count'=>$count,'search_userid'=>$search_userid,'search_verify'=>$search_verify]);
    }
    //管理员审核
    public function verify(Request $request){
        $data=$request->all();
        var_dump($data);
        $user=$request->user();
        $customer=Customer::find($data['id']);
        if($user->role='admin'){
            if(!empty($data['verify'])){
                if($data['verify']=='1'){
                    $customer->verify='通过';
                }
                else
                {
                    $customer->verify='未通过';
                }
                if(!empty($data['remark'])){
                    $customer->remark=$data['remark'];
                }
                $customer->save();
                return redirect('/customerlist');
            }
            else
            {
                if(!empty($data['remark'])){
                    $customer->remark=$data['remark'];
                }
                $customer->save();
                print('参数有误!');
            }
        }
    }
}
